import React from 'react';
import { View, CameraRoll, Text, ScrollView, PermissionsAndroid } from 'react-native';
import styles from './styles';

async function requestCameraPermission() {
  try {
    const granted = await PermissionsAndroid.request(
      PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION
    )
    if (granted === PermissionsAndroid.RESULTS.GRANTED) {
      console.log("You can use the location")
      return true;
    } else {
      console.log("location permission denied")
    }
  } catch (err) {
    console.warn(err)
  }
  return false;
}
class Images extends React.Component {
  static navigationOptions = {
    title: '地理位置',
    text: '',
  };
  state = {
    position: {},
  }
  componentDidMount() {
    const para = {
      enableHighAccuracy: true, // 允许高精度定位
      timeout: 20000, // 20秒没得到位置信息停止获取
      maximumAge: 1000, // 定位结果缓存1000毫秒
    }
    requestCameraPermission().then(
        () => {
          navigator.geolocation.getCurrentPosition(this.getPositionResult, this.logError, para);
          this.watchID = navigator.geolocation.watchPosition(this.getPositionResult);
        }
      ).catch(err=>{
      console.log('无权进行操作');
    });
  }

  componentWillUnmount() {
    navigator.geolocation.clearWatch(this.watchID);
  }

  getPositionResult = (position) => {
    console.log(position);
    this.setState({ position });
  }
  logError = (err) => {
    console.log(err);
  }
  formatDate =(timestamp) =>  {
    var now = new Date(timestamp);
    var year = now.getFullYear();       //年
    var month = now.getMonth() + 1;     //月
    var day = now.getDate();            //日

    var hh = now.getHours();            //时
    var mm = now.getMinutes();          //分
    var ss = now.getSeconds();

    var clock = year + "-";

    if(month < 10)
        clock += "0";

    clock += month + "-";

    if(day < 10)
        clock += "0";

    clock += day + " ";

    if(hh < 10)
        clock += "0";

    clock += hh + ":";
    if (mm < 10) clock += '0';
    clock += mm + ":";

    if (ss < 10) clock += '0';
    clock += ss;

    return(clock);
  }
  render() {
    const { position: {coords={},mocked,timestamp} } = this.state;
    return (
      <View style={styles.container}>
        <View style={styles.text}>
          <Text>精度：{coords.accuracy}</Text>
        </View>
        <View style={styles.text}>
          <Text>高度：{coords.altitude}</Text>
        </View>
        <View style={styles.text}>
          <Text>精度：{coords.altitudeAccuracy}</Text>
        </View>
        <View style={styles.text}>
          <Text>方向：{coords.heading}</Text>
        </View>
        <View style={styles.text}>
          <Text>经度：{coords.longitude}</Text>
        </View>
        <View style={styles.text}>
          <Text>纬度：{coords.latitude}</Text>
        </View>
        <View style={styles.text}>
          <Text>速度：{coords.speed}</Text>
        </View>
        <View style={styles.text}>
          <Text>时间：{timestamp ? this.formatDate(timestamp) : ''}</Text>
        </View>
        <View style={styles.text}>
          <Text>来源：{mocked ? '模拟' : '设备'}</Text>
        </View>
      </View>
    )
  }
}

export default Images;
